package com.ash.demo;


import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;

/**
 * @author : Chen Chao
 * @description jedis测试
 * @encoding : UTF-8
 * @date : 2022/2/7 19:47
 * @file : Demo01.java
 * @software : IntelliJ IDEA
 * @since : 1.8
 */
public class JedisTest {
    /**
     * 主机地址 127.0.0.1 也可以
     */
    public static final String HOST="localhost";
    /**
     * 端口号
     */
    public static final Integer PORT=6379;


    public static void main(String[] args) {
        Jedis jedis = new Jedis(HOST, PORT);
        String ping = jedis.ping();
        System.out.println(ping);
    }
    @Test
    public void test01(){
        Jedis jedis = new Jedis(HOST, PORT);
        /*获取所有key*/
        Set<String> keys = jedis.keys("*");
        for (String key : keys) {
            System.out.println(key);
        }
    }
    /**
     * @author chen chao
     */
    @Test
    public void test02(){
        String key="name";
        Jedis jedis = new Jedis(HOST, PORT);
        jedis.set(key,"lucy");
        System.out.println(jedis.get(key));
        System.out.println(jedis.exists(key));
        jedis.expire(key,3);
    }

    /**
     * @author chen chao
     */
    @Test
    public void test03(){
        Jedis jedis = new Jedis(HOST, PORT);

        /*设置多个key*/
        jedis.mset("k1","v1","k2","v2");
        List<String> strings = jedis.mget("k1", "k2");
        strings.forEach(System.out::println);
    }
    /**
     * @author chen chao
     */

    @Test
    public void test04(){
        Jedis jedis = new Jedis(HOST, PORT);
        /*list操作*/
        jedis.lpush("key1", "a", "b", "c");
        List<String> strings = jedis.lrange("key1", 0, -1);
        strings.forEach(System.out::println);
    }
    /**
     * @author chen chao
     */
    @Test
    public void test05(){
        Jedis jedis = new Jedis(HOST, PORT);
        /*操作set*/
        jedis.sadd("key02","a","b","c","a");
        Set<String> key02 = jedis.smembers("key02");
        key02.forEach(System.out::println);
    }
    /**
     * @author chen chao
     */
    @Test
    public void test06(){
        Jedis jedis = new Jedis(HOST, PORT);
        /*操作hash*/
        jedis.hset("ash","age","20");
        System.out.println(jedis.hget("ash", "age"));
    }

    /**
     * @author chen chao
     */
    @Test
    public void test07(){
        Jedis jedis = new Jedis(HOST, PORT);
        /*操作zSet*/
        jedis.zadd("china",100,"shanghai");
        jedis.zadd("china",200,"nanjing");
        Set<String> china = jedis.zrange("china", 0, -1);
        china.forEach(System.out::println);
    }
}
